/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.dao;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import com.cmcc.zysoft.infocollection.model.Company;
import com.starit.common.dao.hibernate.HibernateBaseDaoImpl;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: CompanyPcDao.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-9-4 下午3:02:20
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Repository
public class CompanyPcDao extends HibernateBaseDaoImpl<Company, String> {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 获取企业列表信息.
	 * @param parentCompanyId 父企业ID
	 * @param companyId 当前用户所在企业ID
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> companyTree(String parentCompanyId) {
		String sql = 
				" SELECT comp.*, comp.company_id as id, comp.company_name as 'text', " +
				" (CASE comp.have_child_company WHEN 'N' THEN 'open' WHEN 'Y' THEN 'closed' ELSE 'open' END) AS state, " +
				" (CASE comp.have_child_company WHEN 'N' THEN TRUE WHEN 'Y' THEN FALSE ELSE TRUE END) AS isLeaf, 'folder' AS iconCls, " +
				" (SELECT COUNT(*) FROM tb_c_department d WHERE d.del_flag = '0' AND d.company_id = comp.company_id) AS deptNum, " +
				" (SELECT COUNT(*) FROM tb_c_headship h WHERE h.del_flag = '0' AND h.company_id = comp.company_id) AS headshipNum " +
				" FROM tb_c_company comp " +
				" WHERE comp.del_flag = '0' AND comp.parent_company_id = ? ";
		return this.jdbcTemplate.queryForList(sql, parentCompanyId);
	}
	
	/**
	 * 根据企业名称搜索企业.
	 * @param companyName 企业名称
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> search(String companyName) {
		String sql = "SELECT * FROM tb_c_company comp WHERE comp.del_flag='0' AND comp.company_name LIKE '%" + companyName.trim() + "%'";
		return this.jdbcTemplate.queryForList(sql);
	}
	
	/**
	 * 根据企业ID查询企业信息.
	 * @param companyId 企业ID
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> searchCompByCompId(String companyId) {
		String sql = "SELECT * FROM tb_c_company comp WHERE comp.del_flag='0' AND comp.company_id = ? ";
		return this.jdbcTemplate.queryForList(sql, companyId);
	}
	
	/**
	 * 检查企业代码是否已存在.
	 * @param companyId 企业ID
	 * @param companyCode 企业代码
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> checkCompanyCode(String companyId, String companyCode) {
		String sql = "SELECT a.* FROM tb_c_company a WHERE a.del_flag = '0' AND a.company_code = ? ";
		if(StringUtils.hasText(companyId)) {
			sql += " AND a.company_id != '" + companyId + "'";
		}
		return this.jdbcTemplate.queryForList(sql, companyCode);
	}
	
	/**
	 * 检查企业名称是否已存在.
	 * @param companyId 企业ID
	 * @param companyName 企业名称
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> checkCompanyName(String companyId, String companyName) {
		String sql = "SELECT a.* FROM tb_c_company a WHERE a.del_flag = '0' AND a.company_name = ? ";
		if(StringUtils.hasText(companyId)) {
			sql += " AND a.company_id != '" + companyId + "'";
		}
		return this.jdbcTemplate.queryForList(sql, companyName);
	}
	
	/**
	 * 根据父企业ID获取子企业.
	 * @param parentCompanyId 父企业ID
	 * @return List<Map<String, Object>>
	 */
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> comTree(String parentCompanyId) {
		String hql = "select new Map(com.companyId as id, com.companyName as text, com.haveChildCompany as haveChildCom) from Company com where com.parentCompanyId = ? and com.delFlag = '0' ";
		parentCompanyId = StringUtils.hasText(parentCompanyId) ? parentCompanyId : "0";
		return this.findByHQL(hql, parentCompanyId);
	}
}